﻿Public Class frmThem_CapNhatKho

    Public status As String
    Public IDHangHoa As Integer

    Private Sub frmThem_CapNhatKho_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Dim dt As DataTable
        dt = TapHoaDAO.LayTatCaViTri()
        cbbViTri.DataSource = dt
        cbbViTri.DisplayMember = "vt_NoiVT"
        cbbViTri.ValueMember = "vt_ID"

        dt = TapHoaDAO.LayTatCaDonVi()
        cbbDonVi.DataSource = dt
        cbbDonVi.DisplayMember = "dvt_Loai"
        cbbDonVi.ValueMember = "dvt_ID"

        dt = TapHoaDAO.TatCaNhaCungCap()
        cbbNhaCungCap.DataSource = dt
        cbbNhaCungCap.DisplayMember = "ncc_Ten"
        cbbNhaCungCap.ValueMember = "ncc_ID"

        dt = TapHoaDAO.TatCaLoaiHang()
        cbbLoaiHang.DataSource = dt
        cbbLoaiHang.DisplayMember = "lh_Loai"
        cbbLoaiHang.ValueMember = "lh_ID"

        dt = TapHoaDAO.TatCaXuatXu()
        cbbXuatXu.DataSource = dt
        cbbXuatXu.DisplayMember = "xx_NoiXuatXu"
        cbbXuatXu.ValueMember = "xx_ID"

        If status = "CapNhat" Then
            btnThem.Text = "Cập nhật"
            lbIDHangHoa.Visible = True
            lb_IDHangHoa.Visible = True
            lbIDHangHoa.Text = IDHangHoa.ToString()
            Me.Text = "Cập nhật hàng hóa trong kho"
        ElseIf status = "Them" Then
            btnThem.Text = "Thêm"
            lbIDHangHoa.Visible = False
            lb_IDHangHoa.Visible = False
            Me.Text = "Thêm hàng hóa vào kho"
        End If

    End Sub

    Private Sub btnThem_Click(sender As System.Object, e As System.EventArgs) Handles btnThem.Click
        If txtGia.Text = "" Or txtHinhAnh.Text = "" Or txtSoLuong.Text = "" Or txtTenHang.Text = "" Then
            MessageBox.Show("bạn cần nhập đầy đủ thông tin")
            Return
        End If
        Dim _taphoa As New TapHoa()
        _taphoa.m_strTenHangHoa = txtTenHang.Text
        _taphoa.m_intGia = Integer.Parse(txtGia.Text)
        _taphoa.m_intIDDonVi = Integer.Parse(cbbDonVi.SelectedValue.ToString())
        _taphoa.m_intSoLuong = Integer.Parse(txtSoLuong.Text)
        _taphoa.m_intIDNhaCungCap = Integer.Parse(cbbNhaCungCap.SelectedValue.ToString())
        _taphoa.m_intIDXuatXu = Integer.Parse(cbbXuatXu.SelectedValue.ToString())
        _taphoa.m_intIDLoaiHang = Integer.Parse(cbbLoaiHang.SelectedValue.ToString())
        _taphoa.m_intIDViTri = Integer.Parse(cbbViTri.SelectedValue.ToString())
        _taphoa.m_strHinhAnh = txtHinhAnh.Text

        If status = "Them" Then
            TapHoaDAO.ThemHangHoaVaoKho(_taphoa)
            Me.Close()
            MessageBox.Show("Thêm sản phẩm vào kho thành công!!!")
        ElseIf status = "CapNhat" Then
            _taphoa.m_intIDHangHoa = Integer.Parse(IDHangHoa)
            TapHoaDAO.CapNhatHangHoa(_taphoa)
            Me.Close()
            MessageBox.Show("Cập nhật sản phẩm thành công!!!")
        End If

    End Sub


    Private Sub frmThem_CapNhatKho_FormClosed(sender As System.Object, e As System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed
        frmXemKho.LayHangHoa()
    End Sub

    Private Sub btnThoat_Click(sender As System.Object, e As System.EventArgs) Handles btnThoat.Click
        Me.Close()
    End Sub

    Private Sub btnHinhAnh_Click(sender As System.Object, e As System.EventArgs) Handles btnHinhAnh.Click
        Dim op As New System.Windows.Forms.OpenFileDialog
        op.InitialDirectory = Application.StartupPath
        op.Filter = "Bitmap Files (*.bmp;*.dib)|*.bmp;*.dib|JPEG (*.jpg;*.jpeg;*.jpe;*.jfif)|*.jpg;*.jpeg;*.jpe;*.jfif|Gif (*.gif)|*.gif|TIFF (*.tif;*.tiff)|*.tif;*.tiff|PNG (*.png)|*.png|ICO (*.ico)|*.ico|All Picture Files|*.bmp;*.dib;*.jpg;*.jpeg;*.jpe;*.jfif;*.gif;*.tif;*.tiff;*.png;*.ico |All files (*.*)|*.*"
        op.ShowDialog()
        txtHinhAnh.Text = op.FileName.ToString()
    End Sub
End Class